<template>
  <el-container>
    <!-- <RegionTree style="margin-top: 16px" @on-choose-region="filetrRegion" /> -->
    <el-main>
      <div class="default-main ba-table-box">
        <TableHeader
          :buttons="[
            'refresh',
            'add',
            'edit',
            'remove',
            'comSearch',
            'columnDisplay',
          ]"
          :quick-search-placeholder="placeholder"
          @action="baTable.onTableHeaderAction"
        />
        <!-- 表格 -->
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table ref="tableRef" @action="baTable.onTableAction" />
        <!-- 表单 -->
        <PopupForm v-if="baTable.form.operate" ref="formRef" />
      </div>
    </el-main>
  </el-container>
</template>
<script lang="ts" setup>
import { ref, provide, onMounted, markRaw } from "vue";
import baTableClass from "@/utils/baTable";
import { DeviceModel } from "@/api/backend/index";
import { baTableApi, operatorShowOff } from "@/utils/common"; // 导入表格api方法生成器
import Table from "@/components/table/index.vue";
import TableHeader from "@/components/table/header/index.vue";
import useCurrentInstance from "@/utils/useCurrentInstance";
import { useRouter, useRoute } from "vue-router";
import type { TableRow, TableColumn } from "@/utils/baTable"; //表格列行类型
import PopupForm from "./popupForm.vue";
import { useModel } from "@/store/backend/DeviceM/model";
import { useBomRoute } from "@/store/backend/BomM/bomRoute";

const model = useModel();
const placeholder = ref<string>("模糊搜索");
const tableRef = ref();
const router = useRouter();
const route = useRoute();
const bomRoute = useBomRoute();

const baTable = new baTableClass(
  new baTableApi(DeviceModel),
  {
    pk: "id",
    column: [
      { type: "selection", align: "center", operator: false },
      // { label: "企业ID", prop: "id", align: "center", operator: false },
      {
        label: "设备名称",
        prop: "modelName",
        align: "center",
        operatorPlaceholder: "模糊搜索",
      },
      {
        label: "设备型号",
        prop: "modelCode",
        align: "center",
        operatorPlaceholder: "精确搜索",
      },
      {
        label: "设备图片",
        prop: "pic",
        render: "image",
        operator: false,
      },
      {
        label: "类型",
        prop: "type",
        align: "center",
        operator: false,
      },
      {
        show: operatorShowOff([route.path + "/remove", route.path + "/edit"]),
        label: "操作",
        align: "center",
        width: "130",
        render: "buttons",
        fixed: "right",
        buttons: [
          {
            render: "tipButton",
            name: "info",
            title: "详情",
            text: "",
            type: "primary",
            icon: "fa fa-search-plus",
            class: "table-row-edit",
            disabledTip: false,
            // 按钮是否显示，请返回布尔值
            display: (row: TableRow, field: TableColumn) => {
              return true;
            },
            // 按钮是否禁用，请返回布尔值
            disabled: (row: TableRow, field: TableColumn) => {
              return false;
            },
            attr: {},
          },
          {
            render: "tipButton",
            name: "edit",
            title: "编辑",
            text: "",
            type: "primary",
            icon: "fa fa-pencil",
            class: "table-row-edit",
            disabledTip: false, // 按钮是否显示，请返回布尔值
            display: (row: TableRow, field: TableColumn) => {
              return true;
            },
            // 按钮是否禁用，请返回布尔值
            disabled: (row: TableRow, field: TableColumn) => {
              return false;
            },
            attr: {},
          },
          {
            render: "confirmButton",
            name: "remove",
            title: "删除",
            text: "",
            type: "danger",
            icon: "el-icon-DeleteFilled",
            class: "table-row-delete",
            popconfirm: {
              confirmButtonText: "确认",
              cancelButtonText: "取消",
              confirmButtonType: "danger",
              title: "确认删除？",
            },
            disabledTip: false,
            // 按钮是否显示，请返回布尔值
            display: (row: TableRow, field: TableColumn) => {
              return true;
            },
            // 按钮是否禁用，请返回布尔值
            disabled: (row: TableRow, field: TableColumn) => {
              return false;
            },
            attr: {},
          },
        ],
        operator: false,
      },
    ],
    filter: {},
    acceptQuery: false,
  },
  {
    defaultItems: {},
  }
);

//跳转详情页
const infoButtonClick = (id: number, row: any) => {
  console.log("id", id, "row", row);
  // getCarbonDetail(id);
  model.$patch((state: any) => {
    state.single = row;
  });
  //   router.push({ path: "/admin/types/detail" });
};

onMounted(() => {
  //全局事件总线
  const { proxy } = useCurrentInstance();
  //将table的ref给到table类
  baTable.table.ref = tableRef.value;
  baTable.mount();
  baTable.getList(DeviceModel);

  /**
   * 表格内的按钮响应
   * @param name 按钮name
   * @param row 被操作行数据
   */
  proxy.eventBus.on(
    "onTableButtonClick",
    (data: { name: string; row: TableRow | any }) => {
      if (!baTable.activate) return;
      //回滚功能暂无
      if (data.name == "rollback") {
        // onRollback([data.row[baTable.table.pk!]])
      } else if (data.name == "info") {
        // 主键字段 pk: "name",
        infoButtonClick(
          (data.row as TableRow)[baTable.table.pk!],
          data.row as any
        );
      }
    }
  );
});

// 实例化表格后，将 baTable 的实例提供给上下文
provide("baTable", baTable);
provide("bomRoute", bomRoute);
</script>
<style lang="scss" scoped>
.el-main {
  padding: 0;
}
</style>
